<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/aaa/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/aaa/layuiadmin/style/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <!------------------------------搜索------------------------->
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">车辆名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="telphone" id="goodsname" placeholder="请输入" autocomplete="off"
                               class="layui-input">
                    </div>
                </div>
                <div class="layui-inline" id="selectName" style="display: none">
                    <label class="layui-form-label">门店名称</label>
                    <div class="layui-input-block" style="width: 200px">
                        <select name="selectSchoolName" id="selectSchoolName">
                            <option value="-1">请选择门店</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <button id="newcar-searchbtn" class="layui-btn layuiadmin-btn-admin" lay-submit
                            lay-filter="LAY-user-back-search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                </div>
            </div>
        </div>
        <!------------------------------添加车辆------------------------->
        <div class="layui-card-body">
            <div style="padding-bottom: 10px;">
                <button class="layui-btn layuiadmin-btn-useradmin" id="newcar-add">添加</button>
            </div>
            <!------------------------------生成的数据表格------------------->
            <table class="layui-hide" id="newcar" lay-filter="newcar-filter"></table>
            <!-----------------生成的数据表格中的功能键按钮------------------->
            <script type="text/html" id="newcar-bar">
                <a class="layui-btn layui-btn-green layui-btn-xs" lay-event="newcar-edit">编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="newcar-addOne">补货</a>
            </script>
        </div>
    </div>
</div>
<!------------------------------添加电瓶车弹框------------------------------------------->
<div id="newcar-layer" style="display: none">
    <form class="layui-form" id="add_carform" style="margin-top: 20px">
        <div class="layui-form-item">
            <label class="layui-form-label">车辆名称</label>
            <div class="layui-input-inline">
                <input type="text" required lay-verify="required" placeholder="车辆名称" id="add_carname" autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">进货价格</label>
            <div class="layui-input-inline">
                <input type="text" required lay-verify="required" placeholder="进货价格" id="add_carmoney"
                       autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">进货数量</label>
            <div class="layui-input-inline">
                <input type="text" required lay-verify="required" placeholder="进货数量" id="add_carnum" autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="formDemo" id="save">保存</button>
            </div>
        </div>
    </form>
</div>
<!------------------------------补货弹框------------------------------------------->
<div id="addcar-layer" style="display: none">
    <form class="layui-form" style="margin-top: 20px">
        <div class="layui-form-item">
            <label class="layui-form-label">补货数量</label>
            <div class="layui-input-inline">
                <input type="text" required lay-verify="required" placeholder="进货数量" id="add_num" autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="formDemo" id="replenishment">保存</button>
            </div>
        </div>
    </form>
</div>
<!------------------------------编辑弹框------------------------------------------->
<div id="editcar-layer" style="display: none">
    <form class="layui-form" style="margin-top: 20px">
        <div class="layui-form-item">
            <label class="layui-form-label">商品名称</label>
            <div class="layui-input-inline">
                <input type="text" required lay-verify="required" placeholder="商品名称" id="editGoodsName" autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="formDemo" id="editBtn">保存</button>
            </div>
        </div>
    </form>
</div>
<script src="/aaa/layuiadmin/layui/layui.js"></script>
<script th:inline="none">
    /*-----------------加载数据表格-------------------------*/
    layui.use(['layer', 'form', 'element', 'table', 'laydate'], function () {
        var $ = layui.$
            , form = layui.form
            , table = layui.table
            , laydate = layui.laydate
            , layer = layui.layer
            , element = layui.element
            , laytpl = layui.laytpl;
        selectSchoolName();    // 调用查询方法
        var $ = layui.$;
        var goodsname;
        var goodscost;
        var isAdmin;
        var scholid;
        table.render({
            elem: '#newcar',
            height: 612,
            url: '/aaa/house/getNewCar',
            page: true,
            cols: [
                [
                    {field: 'goodsname', title: '车辆名称', align: 'center'},
                    {field: 'goodscost', title: '价格', align: 'center'},
                    {field: 'goodsquantity', title: '数量', align: 'center'},
                    {field: 'school', title: '所属门店', align: 'center'},
                    {field: 'schooltelephone', title: '门店电话', align: 'center'},
                    {title: '操作', toolbar: '#newcar-bar', align: 'center', id: 'hidden-bar'},
                ]
            ],
            done: function (res, curr, count) {
                /*--------如果不是管理员的话，隐藏所属门店和门店电话两列-----------*/
                isAdmin = res.isAdmin;
                scholid = res.schoolId;
                if (res.isAdmin != 0) {
                    $("[data-field='schooltelephone']").css('display', 'none');
                    $("[data-field='school']").css('display', 'none');
                } else {
                    /*--------如果是管理员的话，将捕获和新增按钮去掉-----------*/
                    $("#newcar-add").css('display', 'none');
                    $("#selectName").css('display', 'inline-block');
                    table.render({
                        elem: '#newcar',
                        height: 612,
                        url: '/aaa/house/getNewCar',
                        page: true,
                        cols: [
                            [
                                {field: 'goodsname', title: '车辆名称', align: 'center'},
                                {field: 'goodscost', title: '价格', align: 'center'},
                                {field: 'goodsquantity', title: '数量', align: 'center'},
                                {field: 'school', title: '所属门店', align: 'center'},
                                {field: 'schooltelephone', title: '门店电话', align: 'center'},
                            ]
                        ],
                    });
                }
            }
        })

        function selectSchoolName() {
            $.ajax({
                url: "/aaa/house/selectSchoolList",
                type: "GET",
                dataType: "json",
                success: function (result) {
                    var role = document.getElementById("selectSchoolName");        //add_role_name给select定义的id
                    for (var i = 0; i < result.length; i++) {
                        var option = document.createElement("option");    // 创建添加option属性
                        option.setAttribute("value", result[i].schoolId);                  // 给option的value添加值
                        option.innerText = result[i].school;             // 打印option对应的纯文本 （超级管理员、管理员）
                        role.appendChild(option);                          // 给select 添加option子标签
                        form.render("select");                                // 刷性select，显示出数据
                    }
                }
            });
        }

        /*-----------------添加电瓶车-------------------------*/
        $('#newcar-add').click(function () {
            layer.open({
                type: 1,
                title: '新增电瓶车',
                content: $('#newcar-layer')
            });
            $("#add_carname").val("");
            $("#add_carnum").val("");
            $("#add_carmoney").val("");
        })
        /*-----------------添加电瓶车点击保存按钮事件-------------------------*/
        $('#save').click(function () {
            $.post("/aaa/house/addCar", {
                goodsname: $("#add_carname").val(),
                goodscost: $("#add_carmoney").val(),
                goodsquantity: $("#add_carnum").val()
            }, function (data) {
                if (data == 100) {
                    layer.msg("添加成功");
                    layer.closeAll('page'); //关闭弹出框
                    table.reload('newcar'); //重新加载数据
                } else if (data == 204) {
                    layer.msg("车辆名称已存在，不能重复添加，请去补货~");
                    layer.closeAll('page'); //关闭弹出框
                    table.reload('newcar'); //重新加载数据
                } else {
                    layer.msg("添加失败");
                    layer.closeAll('page'); //关闭弹出框
                    table.reload('newcar'); //重新加载数据
                }

            })
            return false;
        });
        /*-----------------搜索电瓶车-------------------------*/
        $('#newcar-searchbtn').click(function () {
            if(isAdmin != 0){
                //是普通员工
                selectSchoolId = scholid;
            }else{
                selectSchoolId =$("#selectSchoolName").val();
            }
            table.reload('newcar', {
                url: '/aaa/house/selectCar',
                where: {
                    goodsname: $("#goodsname").val(),
                    schoolId: selectSchoolId,
                }
            });
            $("#goodsname").val("");
            $("#selectSchoolName").val("-1");
        })

        /*-----------------补货-------------------------*/
        table.on('tool(newcar-filter)', function (obj) {
            $("#add_num").val("");
            var data = obj.data;
            goodsname = data.goodsname;
            goodscost = data.goodscost;
            if (obj.event === 'newcar-addOne') {
                layer.open({
                    type: 1,
                    title: '补货',
                    content: $('#addcar-layer')
                });
            }else if(obj.event === 'newcar-edit'){
                $("#editGoodsName").val(goodsname);
                layer.open({
                    type: 1,
                    title: '编辑',
                    content: $('#editcar-layer')
                });
            }
        });

        /*-----------------补货点击保存按钮事件-------------------------*/
        $('#replenishment').click(function () {
            $.post("/aaa/house/addOneTypeCar", {
                goodsName: goodsname,
                goodsCost: goodscost,
                goodsQuantity: $("#add_num").val()
            }, function (data) {
                if (data == 100) {
                    layer.msg("添加成功");
                    layer.closeAll('page'); //关闭弹出框
                    table.reload('newcar'); //重新加载数据
                } else {
                    layer.msg("添加失败");
                    layer.closeAll('page'); //关闭弹出框
                    table.reload('newcar'); //重新加载数据
                }
            });
            return false;
        })
        /*-----------------编辑点击保存按钮事件-------------------------*/
        $('#editBtn').click(function () {
            $.post("/aaa/house/editOneTypeCar", {
                goodsName: $("#editGoodsName").val(),
                oldGoodsName:goodsname,
            }, function (data) {
                if (data == 100) {
                    layer.msg("更改成功");
                    layer.closeAll('page'); //关闭弹出框
                    table.reload('newcar'); //重新加载数据
                } else {
                    layer.msg("更改失败");
                    layer.closeAll('page'); //关闭弹出框
                    table.reload('newcar'); //重新加载数据
                }
            });
            return false;
        })
    });
</script>
</body>
</html>